//
//  BDLoginViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/12.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "BDLoginViewController.h"
#import "BQActivityView.h"
#import "BDLoginRequest.h"
#import "BDUserAccount.h"
#import "AppDelegate.h"

@interface BDLoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *accountICONImageView;
@property (weak, nonatomic) IBOutlet UIImageView *passwordICONImageView;

@property (weak, nonatomic) IBOutlet UITextField *accountTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextFiled;

@property (weak, nonatomic) IBOutlet UIButton *encryptBtn;


@end

@implementation BDLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.accountTextFiled.delegate = self;
    self.passwordTextFiled.delegate = self;
    
    [self.leftAreaLBtn setImage:nil forState:UIControlStateNormal];
    [self.titleButton setTitle:@"登录" forState:UIControlStateNormal];
    


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)handleGoBackEvent:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (IBAction)loginBtnClcik:(UIButton *)sender {
    [self.view endEditing:YES];
    if (!self.accountTextFiled.text.length) {
        [self showMessage:@"请输入账号"];
        return ;
    }
    
    if (!self.passwordTextFiled.text.length) {
        [self showMessage:@"请输入密码"];
        return ;
    }
    
    __weak typeof(self)weakSelf = self;
    
    [BQActivityView showActiviTy];
    
    BDLoginRequest *task = [[BDLoginRequest alloc] init];
    task.username = self.accountTextFiled.text;
    task.password = self.passwordTextFiled.text;
    
    [BDNetworkManager POST:task completionHander:^(id responseObject, NSString *resultString) {
        [BQActivityView hideActiviTy];

        if (responseObject) {
            [BDUserAccount mj_objectWithKeyValues:responseObject[@"adminUser"]];
            [BDUserAccount saveMyselfToDocuments];
            
            [BDUserAccount restoreMyself];
            NSLog(@"%@\n\n", responseObject);

            NSLog(@"%@ -- %@", [BDUserAccount instatce].username, [BDUserAccount instatce].realname);
            
            //go to home
            [[AppDelegate delfaultDelegate] goToBDHomeViewController];
        }else{
            if (resultString) {
                [weakSelf showMessage:resultString];
            }
        }
    }];
}


- (IBAction)encryptBtnClick:(UIButton *)sender {
    if (self.passwordTextFiled.secureTextEntry) {
        [self.encryptBtn setImage:[UIImage imageNamed:@"dispaly"] forState:UIControlStateNormal];
        self.passwordTextFiled.secureTextEntry = NO;
    }else{
        [self.encryptBtn setImage:[UIImage imageNamed:@"encrypt"] forState:UIControlStateNormal];
        self.passwordTextFiled.secureTextEntry = YES;
    }
}





#pragma mark UITextFieldDelegate

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField ==  self.accountTextFiled) {
        
        self.accountICONImageView.image = [UIImage imageNamed:@"phone_click"];
    }
    
    if (textField == self.passwordTextFiled) {
        
        self.passwordICONImageView.image = [UIImage imageNamed:@"password_click"];
        
    }
    
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField endEditing:YES];
    
    return YES;
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
